<script>
import { VUE_APP_API_URL } from '@/config'
import Vue from 'vue'
// #ifdef H5
var VConsole = require('@/utils/vconsole.min.js')
//  #endif
// #ifdef APP-PLUS
import checkappupdate from '@/utils/checkappupdate.js';
//  #endif
export default {
  onLaunch: function () {
	// #ifdef APP-PLUS
	//版本检测
	checkappupdate.check({
		title:"检测到有新版本！",
		api: VUE_APP_API_URL + '/sy/version'
	})
	
	const jpushModule = uni.requireNativePlugin("JG-JPush"); 
	if (uni.getSystemInfoSync().platform == "ios") {
		// 请求定位权限
		let locationServicesEnabled = jpushModule.locationServicesEnabled()
		let locationAuthorizationStatus = jpushModule.getLocationAuthorizationStatus()
		console.log('locationAuthorizationStatus', locationAuthorizationStatus)
		if (locationServicesEnabled == true && locationAuthorizationStatus < 3) {
			jpushModule.requestLocationAuthorization((result) => {
				console.log('定位权限', result.status)
			})
		}
		jpushModule.requestNotificationAuthorization((result) => {
			let status = result.status
			if (status < 2) {
				uni.showToast({
					icon: 'none',
					title: '您还没有打开通知权限',
					duration: 3000
				})
			}
		})
	}
	jpushModule.setLoggerEnable(true);
	jpushModule.initJPushService()
	jpushModule.addConnectEventListener(result=>{
		let connectEnable = result.connectEnable
		console.log("jpush连接", connectEnable)
	})
	jpushModule.addTagAliasListener(result => {
		let code = result.code
		let sequence = result.sequence
		let tags = result.tags
		let tag = result.tag
		let tagEnable = result.tagEnable
		let alias = result.alias
		console.log(alias, '别名')
	})
	// 通知事件回调
	jpushModule.addNotificationListener(result => {
		let notificationEventType = result.notificationEventType
		let messageID = result.messageID
		let title = result.title
		let content = result.content
		let extras = result.extras
		console.log("通知:", result)
		// #ifdef APP-PLUS
		console.log("通知>>>>:", extras.url)
		plus.runtime.setBadgeNumber(extras.url);//应用图标的右上角消除数字，直接传0即可
		// #endif
		// 收到通知
		// 点击事件
		if (notificationEventType == 'notificationOpened') {
			uni.switchTab({
				url: '/pages/shop/ShoppingCart/index',
				complete(res) {
					console.log(res);
				}
			})
		}else if(notificationEventType == 'notificationArrived') {
			
        }
	})
	jpushModule.getRegistrationID(result => {
		console.log("注册ID", result.registerID)
		if (result.registerID) {
			uni.setStorageSync("register_id", result.registerID)
		}
	})
	jpushModule.addCustomMessageListener(result => {
		let messageID = result.messageID
		let content = result.content
		let extras = result.extras
		console.log("自定义消息", result)
	})
	// #endif
    const updateManager = uni.getUpdateManager()
    updateManager.onCheckForUpdate(function (res) {
      // 请求完新版本信息的回调
      console.log(res.hasUpdate)
    })
    // 下载新版本
    updateManager.onUpdateReady(function () {
      uni.showModal({
        title: '更新提示',
        content: '新版本已经准备好，是否重启应用？',
        success(res) {
          if (res.confirm) {
            // 重启应用
            updateManager.applyUpdate()
          }
        },
      })
    })
    // 新版本下载失败
    updateManager.onUpdateFailed(function (res) {
      // 新的版本下载失败
      uni.showModal({
        title: '已经有新版本了哟~',
        content: '新版本已经上线啦~，请您删除当前小程序，重新搜索打开哟~',
      })
    })
  },
  onShow: function() {
  },
  onHide: function () {
	  
  },
  mounted() {
    this.setAppInfo()
  },
  methods: {
    // 获取系统栏高度
    async setAppInfo() {
      let that = this
      return new Promise((resolve, reject) => {
        uni.getSystemInfo({
          success: function (e) {
            Vue.prototype.StatusBar = e.statusBarHeight
            // #ifdef H5
            Vue.prototype.CustomBar = e.statusBarHeight + 45
            // #endif

            // #ifdef APP-PLUS
            if (e.platform == 'android') {
              Vue.prototype.CustomBar = e.statusBarHeight + 50
            } else {
              Vue.prototype.CustomBar = e.statusBarHeight + 45
            }
            // #endif

            // #ifdef MP-WEIXIN
            let custom = wx.getMenuButtonBoundingClientRect()
            Vue.prototype.Custom = custom
            Vue.prototype.CustomBar = custom.bottom + custom.top - e.statusBarHeight
            // #endif
			
			// #ifdef MP-ALIPAY
			Vue.prototype.StatusBar = e.statusBarHeight;
			Vue.prototype.CustomBar = e.titleBarHeight;
			// #endif
          },
        })
      })
    },

    // 自动登录
    async autoLogin(data) {},
  },
}
</script>

<style lang="less">
/*每个页面公共css */
@import './assets/iconfont/iconfont.css';
@import './assets/css/base.less';
@import './assets/css/reset.less';
@import './assets/css/style.less';

// #ifndef H5
uni-tabbar .uni-tabbar__item:nth-child(3n+1) img{
	width: 146rpx !important;
	height: 148rpx !important;
	margin-top: -0.9*100rpx;
	margin-left:-0.5*100rpx;
}
// #endif
/* 解决小程序和app滚动条的问题 */
/* #ifdef MP || APP-PLUS  */
::-webkit-scrollbar {
	display: none;
	width: 0 !important;
	height: 0 !important;
	-webkit-appearance: none;
	background: transparent;
	color: transparent;
}
/* #endif */

/* 解决H5 的问题 */
/* #ifdef H5 || MP-ALIPAY*/
uni-scroll-view .uni-scroll-view::-webkit-scrollbar {
	/* 隐藏滚动条，但依旧具备可以滚动的功能 */
	display: none;
	width: 0 !important;
	height: 0 !important;
	-webkit-appearance: none;
	background: transparent;
	color: transparent;
}
/* #endif */

uni-radio .uni-radio-input.uni-radio-input-checked{
	background-color: #f00!important;
	border-color: #f00!important;
	background-clip: content-box!important;
	padding: 7rpx!important;
	box-sizing: border-box;
}
</style>
